package com.tuanzi.loan.web.bean.settlementsystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementCompany;
import com.tuanzi.loan.business.service.settlementsystem.SettlementCompanyService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.SETTLEMENT_BILLING_CONTENT;
import static com.tuanzi.loan.business.constants.DictionaryCode.SETTLEMENT_BILL_TYPE;

/**
 * @author caiwanlin
 * @create 2018-08-16 20:36
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SettlementCompanyInfoBean extends JSFInfoBean<SettlementCompany>{
    @Autowired
    private SettlementCompanyService settlementCompanyService;
    @PostConstruct
    private void init() {
        //生效时间设置
        String id = getParamFromRequest("id");
        if (StringUtils.isNotBlank(id)) {
            $ = settlementCompanyService.findOne(Long.parseLong(id));
        } else {
            $ = new SettlementCompany();
            changeStatusToAdd();
        }
    }

    public String onSave(){
        if (validate() == false) return null;
        String companyName = $.getCompanyName().replace(" ","");
        $.setCompanyName(companyName);
        String bankAccount = $.getBankAccount().replace(" ", "");
        $.setBankAccount(bankAccount);
        if ($.getId() == null){//新增公司时，默认公司的预付金额为0，消耗金额为0
            //判断主体是否存在
            SettlementCompany company = settlementCompanyService.findByCompanyName($.getCompanyName());
            if (company != null){
                addErrorMessage("该主体已经存在") ;
                return null;
            }
            $.setPrepayAmount(0.0);
            $.setExpendAmount(0.0);
        }
        cleanNotUseData();
        $ = settlementCompanyService.saveOrUpdate($);
        changeStatusToView();
        addSuccessfullyMessage();
        return redirect("/views/settlementsystem/company-list.xhtml");
    }

    public boolean validate(){
        Boolean isBill = $.getIsBill();
        if (isBill){
            if ($.getBillContent() == null){
                addErrorMessage("开票内容为空");
                return false;
            }
            if ($.getBillType() == null){
                addErrorMessage("发票类型为空");
                return false;
            }
        }
        else {
            if (StringUtils.isBlank($.getBankAccount())){
                addErrorMessage("开户账户为空");
                return false;
            }
        }
        return true;
    }

    public void cleanNotUseData(){
        if ($.getIsBill() == false){
            $.setBillContent(null);
            $.setBillType(null);
            $.setTaxNumber(null);
            $.setBankAddress(null);
            $.setBankNumber(null);
            $.setBillAddress(null);
            $.setReceiveAddress(null);
            $.setReceiveName(null);
            $.setReceivePhone(null);
        }
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return  Lists.newArrayList(SETTLEMENT_BILLING_CONTENT,SETTLEMENT_BILL_TYPE);
    }
}
